<template>
  <div>
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item label="一级分类">
        <el-select
          v-model="formInline.category1Id"
          placeholder="请选择"
          value=""
          @change="getCategory2Id"
          :disabled="!show"
        >
          <el-option
            :label="i.name"
            :value="i.id"
            v-for="i in List1"
            :key="i.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="二级分类">
        <el-select
          v-model="formInline.category2Id"
          placeholder="请选择"
          value=""
          @change="getCategory3Id"
           :disabled="!show"
        >
          <el-option
            :label="i.name"
            :value="i.id"
            v-for="i in List2"
            :key="i.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="三级分类">
        <el-select
          v-model="formInline.category3Id"
          placeholder="请选择"
          value=""
          @change="handle"
           :disabled="!show"
        >
          <el-option
            :label="i.name"
            :value="i.id"
            v-for="i in List3"
            :key="i.id"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "Categoryselect",
  data() {
    return {
      formInline: {
        category1Id: "",
        category2Id: "",
        category3Id: "",
      },
      List1: [],
      List2: [],
      List3: [],
    };
  },
  props:["show"],
  mounted() {
    this.getCategoey1Id();
  },
  methods: {
    async getCategoey1Id() {
      let result = await this.$API.attr.getCategory1Id();
      if (result.code == 200) {
        this.List1 = result.data;
      }
    },
    async getCategory2Id() {
      this.formInline.category2Id = "";
      this.formInline.category3Id = "";
      this.List2 = [];
      this.List3 = [];
      const { category1Id } = this.formInline;
      let result = await this.$API.attr.getCategory2Id(category1Id);
      if (result.code == 200) {
        this.List2 = result.data;
      }
    },
    async getCategory3Id() {
      this.List3 = [];
      this.formInline.category3Id = "";
      const { category2Id } = this.formInline;
      let result = await this.$API.attr.getCategory3Id(category2Id);
      if (result.code == 200) {
        this.List3 = result.data;
      }
    },
    handle() {
      this.$emit("getCategorys", this.formInline);
    },
  },
};
</script>

<style>
</style>